import akshare as ak
import MysqlHelper as sqlHelper
import queue
import threading

class Shareholder:
    def __init__(self):
        self.resource_queue = queue.Queue()
    def top10(self):
        # sql = "select * from base_stock"
        # db = sqlHelper.MysqlHelper()
        # dbstock = db.select(sql)
        # for item in dbstock:
        newStock = ak.stock_info_sh_name_code()
        # newStock = ak.stock_info_sz_name_code()
        for row in newStock.iterrows():
            code = row[1]["证券代码"]
            # code = row[1]["A股代码"]
            if code[:2]=="60":
                code="sh"+code
            elif code[:2]=="00":
                code = "sz" + code
            else:
                continue
            self.resource_queue.put(code)

        for i in range(5):
            t = threading.Thread(name="线程" + str(i), target=self.consumer, )
            t.start()
            # code = item[1][7:] + item[1][:6]
            # print(code)
            stock_gdfx_free_top_10_em_df = None
            # try:
            #     stock_gdfx_free_top_10_em_df = ak.stock_gdfx_top_10_em(symbol=code, date="20240630")
            #     # print(stock_gdfx_free_top_10_em_df)
            # except Exception as e:
            #     stock_gdfx_free_top_10_em_df = ak.stock_gdfx_top_10_em(symbol=code, date="20240331")
            # shareholder = ""
            # if not stock_gdfx_free_top_10_em_df.empty:
            #     for row in stock_gdfx_free_top_10_em_df.iterrows():
            #         if row[1].get('股东名称')[:6] == "全国社保基金": #row[1].get('股东名称') == "香港中央结算有限公司" or
            #             shareholder += "\n\t" + row[1].get('股东名称') + ">" + row[1].get('增减')
            # if shareholder != "":
            #     shareholder = code + shareholder
            #     print(shareholder)

    def consumer(self):
        while True:
            code = self.resource_queue.get()
            try:
                try:
                    stock_gdfx_free_top_10_em_df = ak.stock_gdfx_top_10_em(symbol=code, date="20240630")
                except Exception:
                    stock_gdfx_free_top_10_em_df = ak.stock_gdfx_top_10_em(symbol=code, date="20240331")
            except Exception as e:
                print(f"{code}查询数据出错")
            shareholder = ""
            if not stock_gdfx_free_top_10_em_df.empty:
                index = 0
                for row in stock_gdfx_free_top_10_em_df.iterrows():
                    if row[1].get('股东名称')[:6] == "全国社保基金" or row[1].get('股东名称') == "香港中央结算有限公司": #row[1].get('股东名称') == "香港中央结算有限公司" or
                        result=self.holderStatus(row[1].get('增减'))
                        if result==1:
                            index=index+self.holderStatus(row[1].get('增减'))
                            shareholder += "\n\t" + row[1].get('股东名称') + ">" + row[1].get('增减')
                        # shareholder += "\n\t" + row[1].get('股东名称') + ">" + row[1].get('增减')

                if index>=2:
                    shareholder = code + shareholder
                    print(shareholder)

    def holderStatus(self,val):
        try:
            intVal = int(val)
            if(intVal>0):
                return 1
        except ValueError as e:
            return 1
        return 0
        # if status.isdigit():
        #     if int(status) > 0:
        #         return 1
        #     else:
        #         return 0
        # else:
        #     return 1